﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace TCM.Controllers
{
    public class ExamController : Controller
    {
        private TCMContext db = new TCMContext();        

        //
        // GET: /Exam/

        public ActionResult Index()
        {
            var exa = db.EXAMBASEs.ToList();
            foreach (var item in exa)
            {
                if (item.TYPE != null)
                {
                    var typename =
                db.MAPSTRINGBASEs
                    .Where<MAPSTRINGBASE>(x => x.MAPSTRINGID.Equals(new Guid( item.TYPE)))
                    .FirstOrDefault()
                    .DISPLAYNAME;
                    item.TYPEName = typename;
                }               
            }
            return View(db.EXAMBASEs.ToList ());
        }

        //
        // GET: /Exam/Details/5

        public ActionResult Details(Guid? id = null )
        {
            EXAMBASE exambase = db.EXAMBASEs.Find(id);
            if (exambase == null)
            {
                return HttpNotFound();
            }
            return View(exambase);
        }

        //
        // GET: /Exam/Create

        public ActionResult Create()
        {
            ViewBag.STATUSID = new SelectList(db.MAPSTRINGBASEs
                .Where<MAPSTRINGBASE>
                (x => x.ATTRIBUTENAME.Equals("statusid") &&
                    x.ENTITYNAME.Equals("EXAMBASE")).ToList().AsEnumerable(), "MAPSTRINGID", "DISPLAYNAME");

            ViewBag.TYPE = new SelectList(db.MAPSTRINGBASEs
                .Where<MAPSTRINGBASE>
                (x => x.ATTRIBUTENAME.Equals("TYPE") &&
                    x.ENTITYNAME.Equals("EXAMBASE")).ToList().AsEnumerable(), "MAPSTRINGID", "DISPLAYNAME");

            ViewBag.PLACEMENTTEST = new SelectList(db.MAPSTRINGBASEs
                .Where<MAPSTRINGBASE>
                (x => x.ATTRIBUTENAME.Equals("PLACEMENTTEST") &&
                    x.ENTITYNAME.Equals("EXAMBASE")).ToList().AsEnumerable(), "MAPSTRINGID", "DISPLAYNAME");
            return View();
        }

        //
        // POST: /Exam/Create

        [HttpPost]
        public ActionResult Create(EXAMBASE exambase)
        {
            if (ModelState.IsValid)
            {
                exambase.EXAMID = Guid.NewGuid();
                db.EXAMBASEs.Add(exambase);
                db.SaveChanges();
                return RedirectToAction("Index");
            }
            ViewBag.STATUSID = new SelectList(db.MAPSTRINGBASEs
               .Where<MAPSTRINGBASE>
               (x => x.ATTRIBUTENAME.Equals("statusid") &&
                   x.ENTITYNAME.Equals("EXAMBASE")).ToList().AsEnumerable(), "MAPSTRINGID", "DISPLAYNAME");

            ViewBag.TYPE = new SelectList(db.MAPSTRINGBASEs
                .Where<MAPSTRINGBASE>
                (x => x.ATTRIBUTENAME.Equals("TYPE") &&
                    x.ENTITYNAME.Equals("EXAMBASE")).ToList().AsEnumerable(), "MAPSTRINGID", "DISPLAYNAME");

            ViewBag.PLACEMENTTEST = new SelectList(db.MAPSTRINGBASEs
               .Where<MAPSTRINGBASE>
               (x => x.ATTRIBUTENAME.Equals("PLACEMENTTEST") &&
                   x.ENTITYNAME.Equals("EXAMBASE")).ToList().AsEnumerable(), "MAPSTRINGID", "DISPLAYNAME");
            return View(exambase);
        }

        //
        // GET: /Exam/Edit/5

        public ActionResult Edit(Guid? id = null)
        {
            EXAMBASE exambase = db.EXAMBASEs.Find(id);
            if (exambase == null)
            {
                return HttpNotFound();
            }

            ViewBag.STATUSID = new SelectList(db.MAPSTRINGBASEs
               .Where<MAPSTRINGBASE>
               (x => x.ATTRIBUTENAME.Equals("statusid") &&
                   x.ENTITYNAME.Equals("EXAMBASE")).ToList().AsEnumerable(), "MAPSTRINGID", "DISPLAYNAME", exambase.STATUSID);

            ViewBag.TYPE = new SelectList(db.MAPSTRINGBASEs
                .Where<MAPSTRINGBASE>
                (x => x.ATTRIBUTENAME.Equals("TYPE") &&
                    x.ENTITYNAME.Equals("EXAMBASE")).ToList().AsEnumerable(), "MAPSTRINGID", "DISPLAYNAME", exambase.TYPE);

            ViewBag.PLACEMENTTEST = new SelectList(db.MAPSTRINGBASEs
              .Where<MAPSTRINGBASE>
              (x => x.ATTRIBUTENAME.Equals("PLACEMENTTEST") &&
                  x.ENTITYNAME.Equals("EXAMBASE")).ToList().AsEnumerable(), "MAPSTRINGID", "DISPLAYNAME",exambase.PLACEMENTTEST);

            return View(exambase);
        }

        //
        // POST: /Exam/Edit/5

        [HttpPost]
        public ActionResult Edit(EXAMBASE exambase)
        {
            if (ModelState.IsValid)
            {
                db.Entry(exambase).State = EntityState.Modified;
                db.SaveChanges();
                return RedirectToAction("Index");
            }
            ViewBag.STATUSID = new SelectList(db.MAPSTRINGBASEs
                .Where<MAPSTRINGBASE>
                (x => x.ATTRIBUTENAME.Equals("statusid") &&
                    x.ENTITYNAME.Equals("EXAMBASE")).ToList().AsEnumerable(), "MAPSTRINGID", "DISPLAYNAME",exambase.STATUSID);

            ViewBag.TYPE = new SelectList(db.MAPSTRINGBASEs
                .Where<MAPSTRINGBASE>
                (x => x.ATTRIBUTENAME.Equals("TYPE") &&
                    x.ENTITYNAME.Equals("EXAMBASE")).ToList().AsEnumerable(), "MAPSTRINGID", "DISPLAYNAME", exambase.TYPE);

            ViewBag.PLACEMENTTEST = new SelectList(db.MAPSTRINGBASEs
              .Where<MAPSTRINGBASE>
              (x => x.ATTRIBUTENAME.Equals("PLACEMENTTEST") &&
                  x.ENTITYNAME.Equals("EXAMBASE")).ToList().AsEnumerable(), "MAPSTRINGID", "DISPLAYNAME", exambase.PLACEMENTTEST);

            return View(exambase);
        }

        //
        // GET: /Exam/Delete/5

        public ActionResult Delete(Guid? id = null  )
        {
            EXAMBASE exambase = db.EXAMBASEs.Find(id);
            if (exambase == null)
            {
                return HttpNotFound();
            }
            //ViewBag.STATUSID = new SelectList(db.MAPSTRINGBASEs
            //    .Where<MAPSTRINGBASE>
            //    (x => x.ATTRIBUTENAME.Equals("statusid") &&
            //        x.ENTITYNAME.Equals("EXAMBASE")).ToList().AsEnumerable(), "MAPSTRINGID", "DISPLAYNAME", exambase.STATUSID);
            
           //var st = from MAPSTRINGBASE  MapStr in MAPSTRINGBASE  select MapStr.DISPLAYNAME ;
           // Guid g = Guid.Parse("803caef7-209e-44c4-b99d-4cb2f6768f03");
            

//var result = (from p in result
//              where (p.Prescription.Patient.last_name.Contains(foo) && 
//                     p.Prescription.Patient.first_name.Contains(foo2))
//              select p).Count(); 

           ViewData["STATUSID"] = db.MAPSTRINGBASEs
               .Where<MAPSTRINGBASE>
               (x => x.MAPSTRINGID == exambase.STATUSID).Select(x => x.DISPLAYNAME);

            return View(exambase);
        }

        //
        // POST: /Exam/Delete/5

        [HttpPost, ActionName("Delete")]
        public ActionResult DeleteConfirmed(Guid id)
        {
            EXAMBASE exambase = db.EXAMBASEs.Find(id);
            db.EXAMBASEs.Remove(exambase);
            db.SaveChanges();
            return RedirectToAction("Index");
        }

        protected override void Dispose(bool disposing)
        {
            db.Dispose();
            base.Dispose(disposing);
        }
    }
}